# Copyright (c) 2009 Peter Houghton 
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# WATIR GRID

require 'rubygems'
require 'test/unit'
require 'yaml'
require File.expand_path(File.dirname(__FILE__) + "/../lib/watirgrid")
require 'net/http'
require 'uri'
require 'drb'


class Grid_http_server_tests < Test::Unit::TestCase
  
    # Start the http server
    #
    def setup
      @path = File.expand_path(File.dirname(__FILE__) + "/../test")
      @config_file_3= @path + "/" + "grid_servers_test3.yaml" # 3 valid servers
      @url = URI.parse('http://localhost:7777')
	    @agridhubserver = WatirGrid::GridHubServer.new(@config_file_3, 7777)
      @agridhubserver.debug=true
      
      @server_thread = Thread.new do
        @agridhubserver.start
      end # thread
    end # end setup
	  
	  # Kill off the http server
	  #
	  def teardown
	    @agridhubserver.stop
      @server_thread.kill
	  end # end teardown/clearup
	
	  # Test the server_list method, should return a yaml list of servers.
	  # Check that its the correct number of servers.
	  #
	  def test_grid_http_server_list

      check_num_servers(3,0)
	  end # end test


    # Check the number of servers, and those that are locked.
    # useful for checking that locks have worked etc.
    #
    def check_num_servers(target_num_of_servers, target_num_locked)
      response = Net::HTTP.start(@url.host, @url.port) do |http|
	      http.get('/gridhubserver/server_list')
	    end # http
	    server_list = YAML::load(response.body)
	    server_count=0
	    locked_count=0
	    
      YAML::each_document(response.body) do |doc|
	      server_count += 1
	      # Take the returned yaml, extract the details and check for a locked=true
	      if doc.shift[1]["locked"]==true
	        locked_count += 1
  	    end # if
	    end # each doc
	    assert_equal(target_num_of_servers , server_count , "Check correct number of servers is returned.")
	    assert_equal(target_num_locked , locked_count , "Check correct number of locked servers is returned.")
	    
    end # method


	  # Test the get_server and release_server methods, should lock and unlock a server.
	  #
	  def test_grid_http_get_server_lock_release

      # Lock
	    response = Net::HTTP.start(@url.host, @url.port) do |http|
	      http.get('/gridhubserver/get_server/ie7_xp')
	    end # http
      response_hash = YAML::load(response.body)["grid_response"]

      assert_equal("local_vm3" , response_hash["name"] , "Check that correct server is returned")
      assert_equal(true , response_hash["locked"] , "Check that server is locked is returned")
      assert_equal("192.168.10.12" , response_hash["address"] , "Check that server ip returned")
      assert_equal(7779 , response_hash["port"] , "Check that server port is returned")

      # Check there was 3 servers 1 of which is locked
      check_num_servers(3,1)

      # Release Lock
      response = Net::HTTP.start(@url.host, @url.port) do |http|
	      http.get('/gridhubserver/release_server/local_vm3')
	    end # http
      response_hash = YAML::load(response.body)["grid_response"]
      
      assert_equal("local_vm3" , response_hash["name"] , "Check that correct server is returned")
      assert_equal(false , response_hash["locked"] , "Check that server is locked is returned")
	    
	  end # end test

	  # Test the get browser client methods, should raise when non-existant browser key is requested
	  #
	  def test_grid_client_lock_fail
	      check_num_servers(3,0)
        grid_client = WatirGrid::GridClient.new
        assert_raises RuntimeError do
          grid_client.get_browser("XXXXXXXX"  , @url.host , @url.port)  
        end # assert
        
    end # end test

	  # Test the get browser client methods, should lock and unlock a server, transparently to the client.
	  #
	  def test_grid_client_lock_release
	      check_num_servers(3,0)
        grid_client = WatirGrid::GridClient.new
        browser_obj = grid_client.get_browser("ie6_xp"  , @url.host , @url.port)
        assert_equal(DRbObject.new(nil).class , browser_obj.class , "Check a DRB object is returned by get browser")
        check_num_servers(3,1)
        
        # Release lock
        grid_client.release_browser
        check_num_servers(3,0)
        
	  end # end test

	end # end class
